package com.example.demo.use_demo.a_water_mark;

import cn.hutool.core.io.IoUtil;
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.documents.WatermarkLayout;
import com.spire.doc.fields.ShapeObject;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.PortionEx;
import com.spire.presentation.Presentation;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.xls.ViewMode;
import com.spire.xls.Worksheet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 *  java使用spire实现对图片、pdf、doc、xsl、ppt添加水印
 */
public class WaterMarkUtil {

    /**
     * 缓存区路径
     */
    public static String OUT_PATH = "C:\\Users\\farben\\Desktop\\";
    //水印文字颜色
    private static Color COLOR = Color.LIGHT_GRAY;
    //背景颜色
    private static Color BACK_COLOR = Color.white;
    //水印透明度
    private static float ALPHA = 0.5f;
    //旋转角度
    private static Integer DEGREE = -45;
    //字号大小
    private static int FONT_SIZE = 30;
    //水印文字字体
    private static Font FONT = new Font("仿宋", Font.BOLD, FONT_SIZE);

    private static final Logger logger = LoggerFactory.getLogger(WaterMarkUtil.class);

    /**
     * 读取缓存区的文件
     *
     * @param path 文件路径
     */
    public static MultipartFile fileToMultipart(String path) {
        File file = new File(path);
        //无参构造，文件大小最大值便是默认值10kb，超过10Kb便会通过生成临时文件转化不占用内存
        FileItemFactory factory = new DiskFileItemFactory();
        FileItem item = factory.createItem("textField", "text/plain", true, file.getName());
        int bytesRead;
        byte[] buffer = new byte[path.length()];
        try (FileInputStream fis = new FileInputStream(file);
             OutputStream os = item.getOutputStream()) {
            while ((bytesRead = fis.read(buffer, 0, path.length())) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            logger.info("文件转化失败, fileUrl: " + file.getName(), e);
            return null;
        }
        return new CommonsMultipartFile(item);
    }

    //============================================1、图片添加动态文字水印==================================================

    /**
     * 图片添加文字水印
     *
     * @param logoText 水印文字
     * @param srcFile  原图片文件
     */
    public static MultipartFile setWaterMarkToImageByWords(MultipartFile srcFile, String logoText) {
        try {
            // 1、源图片
            InputStream inputImg = srcFile.getInputStream();
            BufferedImage srcImg = ImageIO.read(inputImg);
            //原图宽
            int imgWidth = srcImg.getWidth();
            //原图高
            int imgHeight = srcImg.getHeight();
            BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置样式
            //消除对线段锯齿
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            //消除画图锯齿
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            //消除文字锯齿
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g.drawImage(srcImg, 0, 0, srcImg.getWidth(null), srcImg.getHeight(null), null);
            //文字水印的宽
            JLabel label = new JLabel(logoText);
            FontMetrics metrics = label.getFontMetrics(FONT);
            int width = metrics.stringWidth(label.getText());

            //特别注意：图片原点（0,0）在左上角
            //向左旋转45°，则第一个水印在左下角比较美观
            //横向打水印，以文字水印的宽为间隔
            for (int i = 0; i <= imgWidth; i += width) {
                //纵向打水印，以文字水印的宽为间隔
                for (int j = imgHeight; j >= 0; j -= width) {
                    // 得到画笔对象，基于图片对象打开绘图
                    g = (Graphics2D) buffImg.getGraphics();
                    //4、设置水印文字颜色
                    g.setColor(COLOR);
                    //5、设置水印文字Font
                    g.setFont(FONT);
                    //6、设置水印文字透明度
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
                    //7、设置水印旋转,一定要设置x,y值，否则会有偏移（度数，X,Y）
                    g.rotate(Math.toRadians(DEGREE), i, j);
                    //8、绘制文字,第一参数->设置的内容，后面两个参数->文字在图片上的坐标位置(x,y)
                    g.drawString(logoText, i, j);
                }
            }

            // 9、释放资源
            g.dispose();

            // 获取图片文件名 xxx.png xxx
            String originFileName = srcFile.getOriginalFilename();
            // 获取原图片后缀 png
            int lastSplit = originFileName.lastIndexOf(".");
            String suffix = originFileName.substring(lastSplit + 1);

            //10、输出新图片
            OutputStream outputImg = new FileOutputStream(OUT_PATH + srcFile.getOriginalFilename());
            ImageOutputStream imOut = ImageIO.createImageOutputStream(outputImg);
            ImageIO.write(buffImg, suffix, imOut);
            // 加水印后的文件上传
            srcFile = fileToMultipart(OUT_PATH + srcFile.getOriginalFilename());
        } catch (Exception e) {
            logger.error("图片水印添加异常", e);
        }
        return srcFile;
    }

    public static ByteArrayOutputStream setWaterMarkToImageByWords(InputStream is,String logoText,String fileName) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            // 1、源图片
            BufferedImage srcImg = ImageIO.read(is);
            //原图宽
            int imgWidth = srcImg.getWidth();
            //原图高
            int imgHeight = srcImg.getHeight();
            BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置样式
            //消除对线段锯齿
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            //消除画图锯齿
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            //消除文字锯齿
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g.drawImage(srcImg, 0, 0, srcImg.getWidth(null), srcImg.getHeight(null), null);
            //文字水印的宽
            JLabel label = new JLabel(logoText);
            FontMetrics metrics = label.getFontMetrics(FONT);
            int width = metrics.stringWidth(label.getText());

            //特别注意：图片原点（0,0）在左上角
            //向左旋转45°，则第一个水印在左下角比较美观
            //横向打水印，以文字水印的宽为间隔
            for (int i = 0; i <= imgWidth; i += width) {
                //纵向打水印，以文字水印的宽为间隔
                for (int j = imgHeight; j >= 0; j -= width) {
                    // 得到画笔对象，基于图片对象打开绘图
                    g = (Graphics2D) buffImg.getGraphics();
                    //4、设置水印文字颜色
                    g.setColor(COLOR);
                    //5、设置水印文字Font
                    g.setFont(FONT);
                    //6、设置水印文字透明度
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
                    //7、设置水印旋转,一定要设置x,y值，否则会有偏移（度数，X,Y）
                    g.rotate(Math.toRadians(DEGREE), i, j);
                    //8、绘制文字,第一参数->设置的内容，后面两个参数->文字在图片上的坐标位置(x,y)
                    g.drawString(logoText, i, j);
                }
            }

            // 9、释放资源
            g.dispose();
            //文件类型
            String srcImageType = fileName.substring(fileName.lastIndexOf(".") + 1);
            //10、输出新图片
            ImageIO.write(buffImg, srcImageType, os);
        } catch (Exception e) {
            logger.error("图片水印添加异常", e);
        } finally {
            IoUtil.close(is);
        }
        return os;
    }

    //============================================2、PDF添加动态文字水印==================================================

    /**
     * PDF添加文字水印
     *
     * @param srcPdf   上传文件
     * @param logoText 水印文字
     */
    public static MultipartFile setWaterMarkToPdfByWords(MultipartFile srcPdf, String logoText) {
        try {
            InputStream inputStream = srcPdf.getInputStream();
            PdfDocument pdf = new PdfDocument();
            //加载示例文档
            pdf.loadFromStream(inputStream);
            //true防止中文乱码
            PdfTrueTypeFont pdfTrueTypeFont = new PdfTrueTypeFont(new Font("仿宋", Font.BOLD, 25), true);
            for (Object obj : pdf.getPages()) {
                //当前页
                PdfPageBase page = (PdfPageBase) obj;
                double width = page.getCanvas().getClientSize().getWidth();
                double height = page.getCanvas().getClientSize().getHeight();
                //1、当前页进行区域划分，当前页纵向3列，横向3行，共9个区域
                Dimension2D dimension2D = new Dimension();
                dimension2D.setSize(width / 3, height / 3);
                //2、对区域画水印
                PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
                //透明度
                brush.getGraphics().setTransparency(ALPHA);
                //旋转角度
                brush.getGraphics().rotateTransform(DEGREE);
                //设置区域水印位置，垂直水平居中
                double offsetX = 0;
                double offsetY = brush.getSize().getHeight() / 2;
                brush.getGraphics().translateTransform(offsetX, offsetY);
                //画水印，居中展示：文本、字体、颜色、x、y、居中展示
                brush.getGraphics().drawString(logoText, pdfTrueTypeFont, PdfBrushes.getLightGray(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
                //恢复
                brush.getGraphics().restore();
                //保存格式
                brush.getGraphics().save();
                //3、根据当前页大小，创建新画布
                Rectangle2D loRect = new Rectangle2D.Float();
                loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
                //4、将新画布和水印画入当前页
                page.getCanvas().drawRectangle(brush, loRect);
            }
            //设置缓存区
            pdf.saveToFile(OUT_PATH + srcPdf.getOriginalFilename());
            pdf.close();
            //读取缓存区的文件
            srcPdf = fileToMultipart(OUT_PATH + srcPdf.getOriginalFilename());
        } catch (Exception e) {
            logger.info("pdf添加水印异常: ", e);
        }
        return srcPdf;
    }

    public static ByteArrayOutputStream setWaterMarkToPdfByWords(InputStream is,String logoText) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            PdfDocument pdf = new PdfDocument();
            //加载示例文档
            pdf.loadFromStream(is);
            //true防止中文乱码
            PdfTrueTypeFont pdfTrueTypeFont = new PdfTrueTypeFont(new Font("仿宋", Font.BOLD, 25), true);
            for (Object obj : pdf.getPages()) {
                //当前页
                PdfPageBase page = (PdfPageBase) obj;
                double width = page.getCanvas().getClientSize().getWidth();
                double height = page.getCanvas().getClientSize().getHeight();
                //1、当前页进行区域划分，当前页纵向3列，横向3行，共9个区域
                Dimension2D dimension2D = new Dimension();
                dimension2D.setSize(width / 3, height / 3);
                //2、对区域画水印
                PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
                //透明度
                brush.getGraphics().setTransparency(ALPHA);
                //旋转角度
                brush.getGraphics().rotateTransform(DEGREE);
                //设置区域水印位置，垂直水平居中
                double offsetX = 0;
                double offsetY = brush.getSize().getHeight() / 2;
                brush.getGraphics().translateTransform(offsetX, offsetY);
                //画水印，居中展示：文本、字体、颜色、x、y、居中展示
                brush.getGraphics().drawString(logoText, pdfTrueTypeFont, PdfBrushes.getLightGray(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
                //恢复
                brush.getGraphics().restore();
                //保存格式
                brush.getGraphics().save();
                //3、根据当前页大小，创建新画布
                Rectangle2D loRect = new Rectangle2D.Float();
                loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
                //4、将新画布和水印画入当前页
                page.getCanvas().drawRectangle(brush, loRect);
            }
            //设置缓存区
            pdf.saveToStream(os, com.spire.pdf.FileFormat.PDF);
            pdf.close();
        } catch (Exception e) {
            logger.info("pdf添加水印异常: ", e);
        } finally {
            IoUtil.close(is);
        }
        return os;
    }

    //============================================3、DOCX/DOC添加文字水印==================================================

    /**
     * DOCX/DOC添加文字水印-单个水印
     *
     * @param srcPdf   上传文件
     * @param logoText 水印文字
     */
    public static MultipartFile setWaterMarkToDocByWords01(MultipartFile srcPdf, String logoText) {
        try {
            InputStream inputStream = srcPdf.getInputStream();
            Document doc = new Document();
            doc.loadFromStream(inputStream, FileFormat.Auto);
            //创建TextWatermark实例
            TextWatermark txtWatermark = new TextWatermark();
            //设置文本水印格式
            txtWatermark.setText(logoText);
            //字体
            txtWatermark.setFontName("仿宋");
            //字号
            txtWatermark.setFontSize(FONT_SIZE);
            //颜色
            txtWatermark.setColor(COLOR);
            //文字倾斜角度 此处使用斜对角
            txtWatermark.setLayout(WatermarkLayout.Diagonal);
            //半透明
            txtWatermark.setSemitransparent(true);

            //获取第一节
            Section section = doc.getSections().get(0);
            //将文本水印添加到示例文档
            section.getDocument().setWatermark(txtWatermark);
            //保存文档
            doc.saveToFile(OUT_PATH + srcPdf.getOriginalFilename());
            //读取缓存区的文件
            srcPdf = fileToMultipart(OUT_PATH + srcPdf.getOriginalFilename());

        } catch (Exception e) {
            logger.info("doc添加水印异常: ", e);
        }
        return srcPdf;
    }

    /**
     * DOCX/DOC添加文字水印-多行水印
     *
     * @param srcPdf   上传文件
     * @param logoText 水印文字
     */
    public static MultipartFile setWaterMarkToDocByWords02(MultipartFile srcPdf, String logoText) {
        try {
            InputStream inputStream = srcPdf.getInputStream();
            Document doc = new Document();
            doc.loadFromStream(inputStream, FileFormat.Auto);
            //添加艺术字并设置大小
            ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
            shape.setWidth(150);
            shape.setHeight(20);
            //旋转角度
            shape.setRotation(315);
            //shape.setRotation(Math.toRadians(DEGREE));
            //字体名称
            shape.getWordArt().setFontFamily("仿宋");
            //文本内容
            shape.getWordArt().setText(logoText);
            //填充颜色
            shape.setFillColor(COLOR);
            //设置线条样式
            shape.setLineStyle(ShapeLineStyle.Single);
            //设置笔划颜色
            shape.setStrokeColor(COLOR);
            //设置笔划权重
            shape.setStrokeWeight(1);

            Section section;
            HeaderFooter header;
            for (int n = 0; n < doc.getSections().getCount(); n++) {
                section = doc.getSections().get(n);
                //获取section的页眉
                header = section.getHeadersFooters().getHeader();
                Paragraph paragraph;
                if (header.getParagraphs().getCount() > 0) {
                    //如果页眉有段落，取它第一个段落
                    paragraph = header.getParagraphs().get(0);
                } else {
                    //否则新增加一个段落到页眉
                    paragraph = header.addParagraph();
                }
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        //复制艺术字并设置多行多列位置
                        shape = (ShapeObject) shape.deepClone();
                        //设置艺术字文本内容、位置及样式
                        shape.setVerticalPosition(150 + 200 * i);
                        shape.setHorizontalPosition(20 + 200 * j);
                        paragraph.getChildObjects().add(shape);
                    }
                }
            }
            //保存文档
            doc.saveToFile(OUT_PATH + srcPdf.getOriginalFilename());
            //读取缓存区的文件
            srcPdf = fileToMultipart(OUT_PATH + srcPdf.getOriginalFilename());

        } catch (Exception e) {
            logger.info("doc添加水印异常: ", e);
        }
        return srcPdf;
    }

    public static ByteArrayOutputStream setWaterMarkToDocByWords02(InputStream is,String logoText) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            Document doc = new Document();
            doc.loadFromStream(is, FileFormat.Auto);
            //添加艺术字并设置大小
            ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
            shape.setWidth(150);
            shape.setHeight(20);
            //旋转角度
            shape.setRotation(315);
            //shape.setRotation(Math.toRadians(DEGREE));
            //字体名称
            shape.getWordArt().setFontFamily("仿宋");
            //文本内容
            shape.getWordArt().setText(logoText);
            //填充颜色
            shape.setFillColor(COLOR);
            //设置线条样式
            shape.setLineStyle(ShapeLineStyle.Single);
            //设置笔划颜色
            shape.setStrokeColor(COLOR);
            //设置笔划权重
            shape.setStrokeWeight(1);

            Section section;
            HeaderFooter header;
            for (int n = 0; n < doc.getSections().getCount(); n++) {
                section = doc.getSections().get(n);
                //获取section的页眉
                header = section.getHeadersFooters().getHeader();
                Paragraph paragraph;
                if (header.getParagraphs().getCount() > 0) {
                    //如果页眉有段落，取它第一个段落
                    paragraph = header.getParagraphs().get(0);
                } else {
                    //否则新增加一个段落到页眉
                    paragraph = header.addParagraph();
                }
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        //复制艺术字并设置多行多列位置
                        shape = (ShapeObject) shape.deepClone();
                        //设置艺术字文本内容、位置及样式
                        shape.setVerticalPosition(150 + 200 * i);
                        shape.setHorizontalPosition(20 + 200 * j);
                        paragraph.getChildObjects().add(shape);
                    }
                }
            }
            //保存文档
            doc.saveToStream(os,FileFormat.Auto);
        } catch (Exception e) {
            logger.info("doc添加水印异常: ", e);
        } finally {
            IoUtil.close(is);
        }
        return os;
    }

    //============================================4、XLS/XLSL添加文字水印==================================================

    /**
     * XLSX添加文字水印
     *
     * @param srcFile  上传文件
     * @param logoText 水印文字
     */
    public static MultipartFile setWaterMarkToXlsxByWords(MultipartFile srcFile, String logoText) {
        try {
            com.spire.xls.Workbook wb = new com.spire.xls.Workbook();
            InputStream inputStream = srcFile.getInputStream();
            wb.loadFromStream(inputStream);
            for (int i = 0; i < wb.getWorksheets().getCount(); i++) {
                //当前sheet页
                Worksheet sheet = wb.getWorksheets().get(i);
                //工作簿高
                double height = sheet.getPageSetup().getPageHeight();
                //工作簿宽
                double width = sheet.getPageSetup().getPageWidth();
                //调用DrawText() 方法插入图片
                BufferedImage imgWtrmrk = drawText(logoText, height, width);
                //将图片设置为页眉
                sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);
                sheet.getPageSetup().setCenterHeader("&G");
                //将显示模式设置为Layout
                sheet.setViewMode(ViewMode.Layout);
            }
            //保存文档
            wb.saveToFile(OUT_PATH + srcFile.getOriginalFilename());
            //读取缓存区的文件
            srcFile = fileToMultipart(OUT_PATH + srcFile.getOriginalFilename());
        } catch (Exception e) {
            logger.info("xlsx添加水印异常: ", e);
        }
        return srcFile;
    }

    public static ByteArrayOutputStream setWaterMarkToXlsxByWords(InputStream is,String logoText) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            com.spire.xls.Workbook wb = new com.spire.xls.Workbook();
            wb.loadFromStream(is);
            for (int i = 0; i < wb.getWorksheets().getCount(); i++) {
                //当前sheet页
                Worksheet sheet = wb.getWorksheets().get(i);
                //工作簿高
                double height = sheet.getPageSetup().getPageHeight();
                //工作簿宽
                double width = sheet.getPageSetup().getPageWidth();
                //调用DrawText() 方法插入图片
                BufferedImage imgWtrmrk = drawText(logoText, height, width);
                //将图片设置为页眉
                sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);
                sheet.getPageSetup().setCenterHeader("&G");
                //将显示模式设置为Layout
                sheet.setViewMode(ViewMode.Layout);
            }
            //保存文档
            wb.saveToStream(os);
        } catch (Exception e) {
            logger.info("xlsx添加水印异常: ", e);
        } finally {
            IoUtil.close(is);
        }
        return os;
    }

    /**
     * 绘制图片
     *
     * @param text   水印文字
     * @param height 工作簿高
     * @param width  工作簿宽
     * @return
     */
    private static BufferedImage drawText(String text, double height, double width) {
        //水印文本
        text += "   " + text;
        //定义图片宽度和高度
        BufferedImage img = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB);
        //创建画笔
        Graphics2D loGraphic = img.createGraphics();
        //获取文本size
        FontMetrics loFontMetrics = loGraphic.getFontMetrics(FONT);
        //文字宽
        int liStrWidth = loFontMetrics.stringWidth(text);
        //文字高
        int liStrHeight = loFontMetrics.getHeight();
        //水印背景颜色
        loGraphic.setColor(BACK_COLOR);
        //绘制矩形
        loGraphic.fillRect(0, 0, (int) width, (int) height);

        //如果你调用translate(50, 50)，则绘图的起点将从（0，0）移动到（50，50）。绘制一个矩形时，坐标（0，0）将对应于新的坐标（50，50）。
        //重新设定原点坐标
        loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
        //执行图片旋转，rotate里包含了translate，并还原了原点坐标
        loGraphic.rotate(Math.toRadians(DEGREE), liStrWidth / 2, liStrHeight / 2);
        //还原设定原点坐标
        loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);

        //打3行,分4份
        int row = 3;
        for (int i = row; i > 0; i--) {
            //设置字体
            loGraphic.setFont(FONT);
            //水印字体颜色
            loGraphic.setColor(COLOR);
            //设置水印文字透明度
            loGraphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
            //文本显示样式及位置
            loGraphic.drawString(text, 0, (int) height / 4 * i);
        }
        //关闭画笔
        loGraphic.dispose();
        return img;
    }

    //============================================5、PPT/PPTX添加文字水印==================================================

    /**
     * ppt文件添加水印,实际上只是添加背景图片
     *
     * @param srcFile  上传文件
     * @param logoText 水印文字
     * @return
     */
    public static MultipartFile setWaterMarkToPptxByWords(MultipartFile srcFile,String logoText) {
        try {
            Presentation presentation = new Presentation();
            InputStream inputStream = srcFile.getInputStream();
            presentation.loadFromStream(inputStream,com.spire.presentation.FileFormat.PPTX_2016);
            //设置文本水印的宽和高
            int width = 300;
            int height = 200;
            //水印坐标
            float x = 0;
            float y = 0;
            //给每页PPT打水印
            for (int i = 0; i < presentation.getSlides().size(); i++) {
                //打5行
                for (int j = 0; j < 5; j++) {
                    y = height * j;
                    //打3列
                    for (int k = 0; k < 3; k++) {
                        x = width * k;
                        Rectangle2D.Double rect = new Rectangle2D.Double(x, y, width, height);
                        //添加一个shape到定义区域
                        IAutoShape shape = presentation.getSlides().get(i).getShapes().appendShape(com.spire.presentation.ShapeType.RECTANGLE, rect);
                        //设置shape样式
                        shape.getFill().setFillType(FillFormatType.NONE);
                        shape.getShapeStyle().getLineColor().setColor(Color.WHITE);
                        shape.getShapeStyle().getFillColor().setColor(Color.red);
                        shape.getShapeStyle().getEffectColor().setColor(Color.RED);
                        //旋转角度
                        shape.setRotation(DEGREE);
                        shape.getLocking().setSelectionProtection(true);
                        shape.getLine().setFillType(FillFormatType.NONE);
                        //添加文本到shape
                        shape.getTextFrame().setText(logoText);

                        PortionEx textRange = shape.getTextFrame().getTextRange();
                        //设置文本水印样式
                        textRange.getFill().setFillType(FillFormatType.SOLID);
                        //字体颜色
                        textRange.getFill().getSolidColor().setColor(Color.LIGHT_GRAY);
                        //字体大小
                        textRange.setFontHeight(30);
                    }
                }
            }
            //保存文档
            presentation.saveToFile(OUT_PATH + srcFile.getOriginalFilename(), com.spire.presentation.FileFormat.PPTX_2016);
            //读取缓存区的文件
            srcFile = fileToMultipart(OUT_PATH + srcFile.getOriginalFilename());
        } catch (Exception e) {
            logger.info("ppt添加水印异常: ", e);
        }
        return srcFile;
    }

    public static ByteArrayOutputStream setWaterMarkToPptxByWords(InputStream is, String logoText) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            Presentation presentation = new Presentation();
            presentation.loadFromStream(is,com.spire.presentation.FileFormat.PPTX_2016);
            //如果超过10页，不打水印
            if (presentation.getSlides().size()>10){
                return null;
            }
            //设置文本水印的宽和高
            int width = 300;
            int height = 200;
            //水印坐标
            float x = 0;
            float y = 0;
            //给每页PPT打水印
            for (int i = 0; i < presentation.getSlides().size(); i++) {
                //打5行
                for (int j = 0; j < 5; j++) {
                    y = height * j;
                    //打3列
                    for (int k = 0; k < 3; k++) {
                        x = width * k;
                        Rectangle2D.Double rect = new Rectangle2D.Double(x, y, width, height);
                        //添加一个shape到定义区域
                        IAutoShape shape = presentation.getSlides().get(i).getShapes().appendShape(com.spire.presentation.ShapeType.RECTANGLE, rect);
                        //设置shape样式
                        shape.getFill().setFillType(FillFormatType.NONE);
                        shape.getShapeStyle().getLineColor().setColor(Color.WHITE);
                        shape.getShapeStyle().getFillColor().setColor(Color.red);
                        shape.getShapeStyle().getEffectColor().setColor(Color.RED);
                        //旋转角度
                        shape.setRotation(DEGREE);
                        shape.getLocking().setSelectionProtection(true);
                        shape.getLine().setFillType(FillFormatType.NONE);
                        //添加文本到shape
                        shape.getTextFrame().setText(logoText);

                        PortionEx textRange = shape.getTextFrame().getTextRange();
                        //设置文本水印样式
                        textRange.getFill().setFillType(FillFormatType.SOLID);
                        //字体颜色
                        textRange.getFill().getSolidColor().setColor(Color.LIGHT_GRAY);
                        //字体大小
                        textRange.setFontHeight(30);
                    }
                }
            }
            //保存文档
            presentation.saveToFile(os,com.spire.presentation.FileFormat.PPTX_2016);
        } catch (Exception e) {
            logger.info("ppt添加水印异常: ", e);
        } finally {
            IoUtil.close(is);
        }
        return os;
    }

}